package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        /*
        //1. 查询员工的总条数
        Integer total = empMapper.selectCount();
        //2. 查询分页员工列表
        List<Emp> rows = empMapper.page((page - 1) * pageSize, pageSize);
        //3. 封装数据PageBean
        return new PageBean(total, rows);
        */

        //1.开启分页操作
        PageHelper.startPage(page, pageSize);
        //2.直接调用查询所有（插件帮助我们分页）
        Page<Emp> p = (Page<Emp>) empMapper.list(name, gender, begin, end);
        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void save(Emp emp) {
        //补全基础属性（密码，时间）
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        //直接调用mapper添加
        empMapper.save(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        //补全基础属性
        emp.setUpdateTime(LocalDateTime.now());
        //调用mapper更新数据
        empMapper.update(emp);
    }

    @Override
    //@Log //添加自定义的注解
    public Emp login(Emp emp) {
        Emp e = empMapper.findByUsernameAndPassword(emp.getUsername(), emp.getPassword());
        return e;
    }

}
